﻿using System;
using System.IO;
using UnityEngine;
using Utility;

// Token: 0x020003ED RID: 1005
public class LookAtRotator : MonoBehaviour
{
	// Token: 0x17000268 RID: 616
	// (get) Token: 0x0600191B RID: 6427 RVA: 0x000A2770 File Offset: 0x000A0970
	// (set) Token: 0x0600191C RID: 6428 RVA: 0x000A2778 File Offset: 0x000A0978
	public Vector3 TargetPos
	{
		get
		{
			return this.targetPos;
		}
		set
		{
			this.targetPos = value;
		}
	}

	// Token: 0x17000269 RID: 617
	// (get) Token: 0x0600191D RID: 6429 RVA: 0x000A2784 File Offset: 0x000A0984
	public LookAtRotator.TYPE CalcType
	{
		get
		{
			return this.calcType;
		}
	}

	// Token: 0x1700026A RID: 618
	// (get) Token: 0x0600191E RID: 6430 RVA: 0x000A278C File Offset: 0x000A098C
	// (set) Token: 0x0600191F RID: 6431 RVA: 0x000A2794 File Offset: 0x000A0994
	public bool isInit
	{
		get
		{
			return this._isInit;
		}
		private set
		{
			this._isInit = value;
		}
	}

	// Token: 0x06001920 RID: 6432 RVA: 0x000A27A0 File Offset: 0x000A09A0
	public void Init()
	{
		if (this.isInit)
		{
			return;
		}
		this.baseLocalRot = this.baseBone.transform.localRotation;
		if (this.rotateBones != null && this.rotateBones.Length > 0)
		{
			for (int i = 0; i < this.rotateBones.Length; i++)
			{
				this.rotateBones[i].Init();
			}
		}
		this.isInit = true;
	}

	// Token: 0x06001921 RID: 6433 RVA: 0x000A2818 File Offset: 0x000A0A18
	public void Calc()
	{
		if (this.calcType == LookAtRotator.TYPE.NO)
		{
			return;
		}
		foreach (LookAtRotator.RotateBone rotateBone in this.rotateBones)
		{
			rotateBone.ResetRotate();
		}
		this.baseBone.transform.localRotation = this.baseLocalRot;
		if (this.target && (this.calcType == LookAtRotator.TYPE.TARGET || this.calcType == LookAtRotator.TYPE.AWAY))
		{
			this.targetPos = this.target.transform.position;
		}
		Vector3 vector = this.baseBone.InverseTransformPoint(this.targetPos);
		Vector3 vector2 = Vector3.zero;
		foreach (LookAtRotator.RotateBone rotateBone2 in this.rotateBones)
		{
			if (this.calcType == LookAtRotator.TYPE.NO)
			{
				vector2 = rotateBone2.bone.localEulerAngles;
			}
			else if (this.calcType == LookAtRotator.TYPE.TARGET)
			{
				VectorUtility.Vector3_ToYawPitch(vector, out vector2.y, out vector2.x);
				vector2.y = Mathf.DeltaAngle(0f, vector2.y);
				vector2.x = Mathf.DeltaAngle(0f, vector2.x) + this.offsetPitch;
				if (this.prevTarget != null && Mathf.Abs(vector2.y) >= this.ignoreYaw)
				{
					VectorUtility.Vector3_ToYawPitch(this.prevTarget.Value, out vector2.y, out vector2.x);
					vector2.y = Mathf.DeltaAngle(0f, vector2.y);
					vector2.x = Mathf.DeltaAngle(0f, vector2.x) + this.offsetPitch;
				}
				else
				{
					this.prevTarget = new Vector3?(vector);
				}
				vector2 *= this.turnRate;
				vector2.y = Mathf.Clamp(vector2.y, -rotateBone2.maxLeft, rotateBone2.maxRight);
				vector2.x = Mathf.Clamp(vector2.x, -this.maxUp, this.maxDown);
				vector2.z = 0f;
			}
			else if (this.calcType == LookAtRotator.TYPE.FORWARD)
			{
				vector2.y = 0f;
				vector2.x = this.offsetPitch;
				vector2.y = Mathf.Clamp(vector2.y, -rotateBone2.maxLeft, rotateBone2.maxRight);
				vector2.x = Mathf.Clamp(vector2.x, -this.maxUp, this.maxDown);
				vector2.z = 0f;
			}
			else if (this.calcType == LookAtRotator.TYPE.DIRECTION)
			{
				vector2.y = this.targetPos.y;
				vector2.x = this.targetPos.x + this.offsetPitch;
				vector2.y = Mathf.Clamp(vector2.y, -rotateBone2.maxLeft, rotateBone2.maxRight);
				vector2.x = Mathf.Clamp(vector2.x, -this.maxUp, this.maxDown);
				vector2.z = 0f;
			}
			else if (this.calcType == LookAtRotator.TYPE.DIRECTION_UNLIMITED)
			{
				vector2.y = this.targetPos.y;
				vector2.x = this.targetPos.x + this.offsetPitch;
				vector2.z = 0f;
			}
			else if (this.calcType == LookAtRotator.TYPE.AWAY)
			{
				Vector3 zero = Vector3.zero;
				VectorUtility.Vector3_ToYawPitch(vector, out zero.y, out zero.x);
				zero.y = Mathf.DeltaAngle(0f, zero.y);
				zero.x = Mathf.DeltaAngle(0f, zero.x) + this.offsetPitch;
				if (this.prevTarget != null && Mathf.Abs(zero.y) <= this.awayIgnoreYaw)
				{
					VectorUtility.Vector3_ToYawPitch(this.prevTarget.Value, out zero.y, out zero.x);
					zero.y = Mathf.DeltaAngle(0f, zero.y);
					zero.x = Mathf.DeltaAngle(0f, zero.x) + this.offsetPitch;
				}
				else
				{
					this.prevTarget = new Vector3?(vector);
				}
				vector2.y = ((zero.y <= 0f) ? rotateBone2.maxRight : (-rotateBone2.maxLeft));
				vector2.x = ((zero.x <= 0f) ? this.maxDown : (-this.maxUp));
				vector2.z = 0f;
			}
			if (this.calcType == LookAtRotator.TYPE.HOLD)
			{
				rotateBone2.PrevEuler = rotateBone2.Euler;
				Quaternion rhs = Quaternion.Euler(rotateBone2.Euler);
				Quaternion rot = this.baseLocalRot * rhs;
				rotateBone2.Rotate(rot);
			}
			else
			{
				rotateBone2.Interpolation(vector2, this.turnPower, this.turnMinSpeed, this.turnMaxSpeed);
				rotateBone2.PrevEuler = rotateBone2.Euler;
				Quaternion quaternion = Quaternion.Euler(rotateBone2.Euler);
				Quaternion quaternion2 = this.baseLocalRot * quaternion;
				rotateBone2.Rotate(quaternion);
			}
		}
	}

	// Token: 0x06001922 RID: 6434 RVA: 0x000A2D7C File Offset: 0x000A0F7C
	public void Change(LookAtRotator.TYPE type, Transform tgt, bool force)
	{
		this.calcType = type;
		this.target = tgt;
		if (tgt)
		{
			this.targetPos = tgt.position;
		}
		this.noRate = ((!force) ? 0f : 1f);
		foreach (LookAtRotator.RotateBone rotateBone in this.rotateBones)
		{
			rotateBone.Change();
		}
	}

	// Token: 0x06001923 RID: 6435 RVA: 0x000A2DF0 File Offset: 0x000A0FF0
	public void Change(LookAtRotator.TYPE type, Vector3 tgt, bool force)
	{
		this.calcType = type;
		this.targetPos = tgt;
		this.target = null;
		this.noRate = ((!force) ? 0f : 1f);
		foreach (LookAtRotator.RotateBone rotateBone in this.rotateBones)
		{
			rotateBone.Change();
		}
	}

	// Token: 0x06001924 RID: 6436 RVA: 0x000A2E54 File Offset: 0x000A1054
	public void SetTarget(Transform _target)
	{
		this.target = _target;
		if (_target)
		{
			this.targetPos = _target.position;
		}
	}

	// Token: 0x06001925 RID: 6437 RVA: 0x000A2E74 File Offset: 0x000A1074
	public Transform GetTarget()
	{
		return this.target;
	}

	// Token: 0x06001926 RID: 6438 RVA: 0x000A2E7C File Offset: 0x000A107C
	public void ChangePtn(LookAtRotator.TYPE _type, bool _force = false)
	{
		this.calcType = _type;
		this.noRate = ((!_force) ? 0f : 1f);
		foreach (LookAtRotator.RotateBone rotateBone in this.rotateBones)
		{
			rotateBone.Change();
		}
	}

	// Token: 0x06001927 RID: 6439 RVA: 0x000A2ED0 File Offset: 0x000A10D0
	public void Save(BinaryWriter _bw)
	{
		_bw.Write((int)this.calcType);
		_bw.Write(this.rotateBones.Length);
		foreach (LookAtRotator.RotateBone rotateBone in this.rotateBones)
		{
			rotateBone.Save(_bw);
		}
	}

	// Token: 0x06001928 RID: 6440 RVA: 0x000A2F20 File Offset: 0x000A1120
	public void Load(BinaryReader _br)
	{
		this.Init();
		this.calcType = (LookAtRotator.TYPE)_br.ReadInt32();
		int num = _br.ReadInt32();
		for (int i = 0; i < num; i++)
		{
			this.rotateBones[i].Load(_br);
		}
	}

	// Token: 0x06001929 RID: 6441 RVA: 0x000A2F68 File Offset: 0x000A1168
	private void Start()
	{
		this.Init();
	}

	// Token: 0x0600192A RID: 6442 RVA: 0x000A2F70 File Offset: 0x000A1170
	private void LateUpdate()
	{
		this.Calc();
	}

	// Token: 0x04001C53 RID: 7251
	[SerializeField]
	private Transform baseBone;

	// Token: 0x04001C54 RID: 7252
	[SerializeField]
	private Transform target;

	// Token: 0x04001C55 RID: 7253
	[SerializeField]
	private Vector3 targetPos;

	// Token: 0x04001C56 RID: 7254
	[SerializeField]
	private LookAtRotator.RotateBone[] rotateBones;

	// Token: 0x04001C57 RID: 7255
	private Vector3? prevTarget;

	// Token: 0x04001C58 RID: 7256
	[Range(0f, 90f)]
	[SerializeField]
	private float maxUp = 45f;

	// Token: 0x04001C59 RID: 7257
	[SerializeField]
	[Range(0f, 90f)]
	private float maxDown = 45f;

	// Token: 0x04001C5A RID: 7258
	[SerializeField]
	[Range(0f, 2f)]
	private float turnRate = 1f;

	// Token: 0x04001C5B RID: 7259
	[SerializeField]
	[Range(-45f, 45f)]
	private float offsetPitch;

	// Token: 0x04001C5C RID: 7260
	[SerializeField]
	private float turnPower = 3f;

	// Token: 0x04001C5D RID: 7261
	[SerializeField]
	private float turnMinSpeed = 1f;

	// Token: 0x04001C5E RID: 7262
	[SerializeField]
	private float turnMaxSpeed;

	// Token: 0x04001C5F RID: 7263
	[SerializeField]
	private float ignoreYaw = 120f;

	// Token: 0x04001C60 RID: 7264
	[SerializeField]
	private float awayIgnoreYaw = 45f;

	// Token: 0x04001C61 RID: 7265
	private Quaternion baseLocalRot;

	// Token: 0x04001C62 RID: 7266
	[SerializeField]
	private LookAtRotator.TYPE calcType;

	// Token: 0x04001C63 RID: 7267
	private float noRate;

	// Token: 0x04001C64 RID: 7268
	private bool _isInit;

	// Token: 0x020003EE RID: 1006
	public enum TYPE
	{
		// Token: 0x04001C66 RID: 7270
		NO,
		// Token: 0x04001C67 RID: 7271
		TARGET,
		// Token: 0x04001C68 RID: 7272
		AWAY,
		// Token: 0x04001C69 RID: 7273
		FORWARD,
		// Token: 0x04001C6A RID: 7274
		DIRECTION,
		// Token: 0x04001C6B RID: 7275
		DIRECTION_UNLIMITED,
		// Token: 0x04001C6C RID: 7276
		HOLD
	}

	// Token: 0x020003EF RID: 1007
	[Serializable]
	private class RotateBone
	{
		// Token: 0x1700026B RID: 619
		// (get) Token: 0x0600192C RID: 6444 RVA: 0x000A2F98 File Offset: 0x000A1198
		// (set) Token: 0x0600192D RID: 6445 RVA: 0x000A2FA0 File Offset: 0x000A11A0
		public Vector3 Euler
		{
			get
			{
				return this.euler;
			}
			set
			{
				this.euler = value;
			}
		}

		// Token: 0x1700026C RID: 620
		// (get) Token: 0x0600192E RID: 6446 RVA: 0x000A2FAC File Offset: 0x000A11AC
		// (set) Token: 0x0600192F RID: 6447 RVA: 0x000A2FB4 File Offset: 0x000A11B4
		public Vector3 PrevEuler
		{
			get
			{
				return this.prevEuler;
			}
			set
			{
				this.prevEuler = value;
			}
		}

		// Token: 0x06001930 RID: 6448 RVA: 0x000A2FC0 File Offset: 0x000A11C0
		public void Init()
		{
			this.baseRot = this.bone.localRotation;
			this.euler = Vector3.zero;
		}

		// Token: 0x06001931 RID: 6449 RVA: 0x000A2FE0 File Offset: 0x000A11E0
		public void Change()
		{
			this.prevEuler = this.euler;
		}

		// Token: 0x06001932 RID: 6450 RVA: 0x000A2FF0 File Offset: 0x000A11F0
		public void Rotate(Quaternion rot)
		{
			this.bone.localRotation = this.baseRot * rot;
		}

		// Token: 0x06001933 RID: 6451 RVA: 0x000A300C File Offset: 0x000A120C
		public void ResetRotate()
		{
			this.bone.localRotation = this.baseRot;
		}

		// Token: 0x06001934 RID: 6452 RVA: 0x000A3020 File Offset: 0x000A1220
		public void LerpAngle(Vector3 rel, float noRate)
		{
			this.euler.x = Mathf.LerpAngle(this.prevEuler.x, rel.x, noRate);
			this.euler.y = Mathf.LerpAngle(this.prevEuler.y, rel.y, noRate);
			this.euler.z = Mathf.LerpAngle(this.prevEuler.z, rel.z, noRate);
		}

		// Token: 0x06001935 RID: 6453 RVA: 0x000A3098 File Offset: 0x000A1298
		public void Interpolation(Vector3 rel, float speed, float min, float max)
		{
			this.euler.y = Tween.Spring(this.euler.y, rel.y, speed, Time.deltaTime, min, max);
			this.euler.x = Tween.Spring(this.euler.x, rel.x, speed, Time.deltaTime, min, max);
			this.euler.z = Tween.Spring(this.euler.z, rel.z, speed, Time.deltaTime, min, max);
		}

		// Token: 0x06001936 RID: 6454 RVA: 0x000A3128 File Offset: 0x000A1328
		public void Save(BinaryWriter _bw)
		{
			_bw.Write(JsonUtility.ToJson(this.maxLeft));
			_bw.Write(JsonUtility.ToJson(this.maxRight));
			_bw.Write(JsonUtility.ToJson(this.baseRot));
			_bw.Write(JsonUtility.ToJson(this.euler));
			_bw.Write(JsonUtility.ToJson(this.prevEuler));
		}

		// Token: 0x06001937 RID: 6455 RVA: 0x000A31A4 File Offset: 0x000A13A4
		public void Load(BinaryReader _br)
		{
			JsonUtility.FromJson<float>(_br.ReadString());
			JsonUtility.FromJson<float>(_br.ReadString());
			JsonUtility.FromJson<Quaternion>(_br.ReadString());
			this.euler = JsonUtility.FromJson<Vector3>(_br.ReadString());
			this.prevEuler = JsonUtility.FromJson<Vector3>(_br.ReadString());
		}

		// Token: 0x04001C6D RID: 7277
		public Transform bone;

		// Token: 0x04001C6E RID: 7278
		public float maxLeft = 45f;

		// Token: 0x04001C6F RID: 7279
		public float maxRight = 45f;

		// Token: 0x04001C70 RID: 7280
		private Quaternion baseRot;

		// Token: 0x04001C71 RID: 7281
		private Vector3 euler;

		// Token: 0x04001C72 RID: 7282
		private Vector3 prevEuler;
	}
}
